//
//  LYMineClassViewController.m
//  artist
//
//  Created by Jay_Apple on 16/5/6.
//  Copyright © 2016年 viviyoo. All rights reserved.
//

#import "LYMineClassViewController.h"
#import "LYClassWebViewController.h"
#import "LYCollectListModel.h"
#import "LYPublicClassesTableViewController.h"
#import "LYEArtistCell.h"

@interface LYMineClassViewController ()<VVYRefreshTableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *models;

@property (nonatomic, assign) NSInteger page;

@end

@implementation LYMineClassViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //初始化导航栏
    [self setUpNav];
    
    [self.tableView.mj_header beginRefreshing];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 初始化
/**
 *  初始化导航栏
 */
- (void)setUpNav{

    if([LYDataSourceManager sharedManager].type != roleTypeStu){
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"添加" style:UIBarButtonItemStyleDone target:self action:@selector(addClass)];

    self.navigationItem.rightBarButtonItem = rightItem;
    }
}

/**
 *  添加课程
 */
- (void)addClass{

    //跳转课程发布页面
    if([[LYDataSourceManager sharedManager].userInfo.extra_info.auth_identity isEqualToString:@"0"]){
        [SVProgressHUD showErrorWithStatus:@"没通过认证,不能发布课程"];
        return;
    }
    
    LYPublicClassesTableViewController *publicVC =  [[LYPublicClassesTableViewController alloc]init];
    [self.navigationController pushViewController:publicVC animated:YES];

}

#pragma mark - 网络请求

/**
 *  请求新数据
 */
- (void)loadNewData{
    
    
    [LYRequestTool getMyClassWithToken:LYToken page:@"1" per_page:@"10" success:^(NSArray<LYMyClassListModel *> *models) {
        
        [self.tableView.mj_header endRefreshing];
         [self.tableView.mj_footer resetNoMoreData];
        self.models = [models mutableCopy];
        [self.tableView reloadData];
        self.page = 1;
        
    } failure:^(NSString *code, NSString *message) {
        [self.tableView.mj_header endRefreshing];
    }];
    
}
/**
 *  加载更多数据
 */
- (void)loadMoreData{
    
    NSString *page = [NSString stringWithFormat:@"%zd",self.page + 1];
    NSString *perpage = @"10";
    
    [LYRequestTool getMyClassWithToken:LYToken page:page per_page:perpage success:^(NSArray<LYMyClassListModel *> *models) {
        if (models.count == 0) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
            return;
        }
        
        [self.tableView.mj_footer endRefreshing];
        
        [self.models addObjectsFromArray:models];;
        self.page++;
        [self.tableView reloadData];
        
    } failure:^(NSString *code, NSString *message) {
        
        [self.tableView.mj_footer endRefreshing];
    }];
    
    
}

/**
 *  关闭课程
 */
- (void)closeClassWithClassID:(NSString *)class_id{


    [LYRequestTool closeClassWithToken:LYToken course_id:class_id success:^{
        
        [self.tableView.mj_header beginRefreshing];
        [MBProgressHUD showSuccess:@"关闭成功"];
        
    } failure:^(NSString *code, NSString *message) {
       
        [MBProgressHUD showOnlyText:message];
        
    }];

}

#pragma mark - 上下拉刷新
/**
 *  下拉
 */
- (void)tableViewHeaderRefreshMethod:(VVYRefreshTableView *)tableView{
    
    [self loadNewData];
    
}

/**
 *  上拉
 */
- (void)tableViewfooterRefreshMethod:(VVYRefreshTableView *)tableView{
    
    [self loadMoreData];
    
}


#pragma mark - tableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 80;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LYMyClassListModel *model = self.models[indexPath.row];
    LYClassWebViewController *classWebVC = [LYClassWebViewController classWebVCWithID:model.Id];
    [self.navigationController pushViewController:classWebVC animated:YES];
    
}


//真机下好像要实现这个方法才行
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{

    if ([LYDataSourceManager sharedManager].type == roleTypeStu) {
        return NO;
    }else{
    
        return YES;
    }

}


- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    WS(weakSelf);
    
    LYMyClassListModel *model = self.models[indexPath.row];
    UITableViewRowAction *action = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"关闭课程" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
                
        [weakSelf closeClassWithClassID:model.Id];
        
            }];
    
    action.backgroundColor = [UIColor redColor];
    
    return @[action];
    
}



#pragma mark - tableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.models.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellID = @"LYEArtistCell";
    LYEArtistCell *cell = [LYEArtistCell cellWithTableView:tableView andIndetifier:cellID];
    
    //model
    LYMyClassListModel *model = self.models[indexPath.row];
    cell.model = model;
    
    return cell;
    
}




@end
